﻿using System;
using System.Runtime.InteropServices;
using SolidEdgeFramework;
using System.Text;
using System.Security.Permissions;

[assembly: CLSCompliant(false)]
namespace MultiConvert
{
    [ComImport, Guid("ded89db0-45b6-11ce-b307-0800363a1e02")]
    class SolidEdgeApplication
    {

    }

	public sealed class SEApplicationWrapper : MultiConvertApplicationWrapper
	{
		internal new SolidEdgeFramework.Application Application
		{
			get
			{
				return (SolidEdgeFramework.Application)base.Application;
			}
			set
			{
				base.Application = value;
			}
		}

		internal new SEDocumentConverter Document
		{
			get
			{
				return (SEDocumentConverter)base.Document;
			}
			set
			{
				base.Document = value;
			}
		}

		private SolidEdgeFramework.HighlightSet HighlightSet;
		private SolidEdgeFramework.DISEApplicationEvents_Event SEApplicationEvent;
		private Boolean DisplayAlerts;
		delegate void ErrorHandler(String errMessage);

		public void ResetDisplayAlerts()
		{
			Application.DisplayAlerts = this.DisplayAlerts;
		}
		public void SetDisplayAlertsOff()
		{
			this.DisplayAlerts = Application.DisplayAlerts;
			Application.DisplayAlerts = false;
		}

		[EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
		public SEApplicationWrapper ()
		{
			try
			{
				this.Application = (SolidEdgeFramework.Application)Marshal.GetActiveObject("SolidEdge.Application");
			}
			catch (System.Runtime.InteropServices.COMException COMex)
			{
				#if TRACE
				System.Diagnostics.Trace.WriteLine(COMex.ToString());
				#endif
				
                /*				
                Guid clsid = new Guid("ded89db0-45b6-11ce-b307-0800363a1e02");
				// CLSIDFromProgID("SolidEdge.Application", out clsid);
				IntPtr pUnk = new IntPtr();
				IntPtr ppv = new IntPtr();
				Int32  HRESULT;
				
				HRESULT = Marshal.QueryInterface(pUnk, ref clsid, out ppv); */

				this.Application = (SolidEdgeFramework.Application)new SolidEdgeApplication();
                //this.Application = (SolidEdgeFramework.Application)Microsoft.VisualBasic.Interaction.CreateObject("SolidEdge.Application");                
                
			}
			this.SEApplicationEvent = (SolidEdgeFramework.DISEApplicationEvents_Event)Application.ApplicationEvents;
			this.Application.Visible = true;
		}

		public override void ArrangeApplication()
		{
			this.SetDisplayAlertsOff();
		}

		public override void OpenDocument(String fromFile)
		{
			try
			{
				this.Application.Documents.Open(fromFile);
#if TRACE				
				String DocType = this.Application.ActiveDocumentType.ToString();
#endif				
				switch (this.Application.ActiveDocumentType)
				{
					case DocumentTypeConstants.igPartDocument:					
						Document = new SEPartConverter(this.Application.ActiveDocument, this);
						break;					

					case DocumentTypeConstants.igSheetMetalDocument:
						Document = new SESheetMetalConverter(this.Application.ActiveDocument, this);
						break;
                        					
					case DocumentTypeConstants.igAssemblyDocument:
						Document = new SEAssemblyConverter(this.Application.ActiveDocument, this);
						break;					

					case DocumentTypeConstants.igWeldmentAssemblyDocument:
						Document = new SEAssemblyConverter(this.Application.ActiveDocument, this);
						break;
				    
					case DocumentTypeConstants.igWeldmentDocument:
						Document = new SEWeldmentConverter(this.Application.ActiveDocument, this);
						break;

                    case DocumentTypeConstants.igDraftDocument:
                        Document = new SEDraftConverter(this.Application.ActiveDocument, this);
                        break;
				}
			}


			catch (System.Runtime.InteropServices.COMException ComEx)
			{
				if (ComEx.ErrorCode == unchecked((int)0x80030021))
				{
					throw new ConvertException(String.Format(MultiConvert.Properties.Resources.ExCanNotOpenFile, fromFile));
				}
				else throw new ConvertException(ComEx.ToString(), ComEx);
			}

			catch (Exception ex)
			{
				throw new ConvertException(ex.ToString(), ex);
			}
		}

		public override String ConvertDocument(String convertMethod, String toFileName)
		{
			return this.Document.convert(convertMethod, toFileName);
		}

		public override void CloseDocuments()
		{
            Type type = this.Document.GetType();
            if (type.FullName != "MultiConvert.SEDraftConverter")
            {
                this.RemoveHighlight();                
            }
            this.Application.Documents.Close();
		}

		public override void ReArrangeApplication()
		{
			this.ResetDisplayAlerts();
		}

		public void Highlight(Object[] items)
		{
			System.Type myType;
			if (this.Document.HighlightSets.Count == 0)
			{
				this.Document.HighlightSets.Add();
			}

			this.HighlightSet = this.Document.HighlightSets.Item(this.Document.HighlightSets.Count);
			foreach (Object Item in items)
			{
				try
				{
					myType = Item.GetType();
					if (myType.IsCOMObject)
					{
						this.HighlightSet.AddItem(Item);
					}
					else
					{
						this.HighlightSet.AddItem(((ISEGeometry)Item).getGeometry());
					}
					this.HighlightSet.Draw();
				}
				catch { }

			}
		}

		public void Highlight(Object item)
		{
			try
			{
				System.Type myType = item.GetType(); ;
				if (this.Document.HighlightSets.Count == 0)
				{
					this.Document.HighlightSets.Add();
				}
				this.HighlightSet = this.Document.HighlightSets.Item(this.Document.HighlightSets.Count);
				if (myType.IsCOMObject)
				{
					this.HighlightSet.AddItem(item);
				}
				else
				{
					this.HighlightSet.AddItem(((ISEGeometry)item).getGeometry());
				}
				this.HighlightSet.Draw();
			}
			catch { }
		}

		public void RemoveHighlight()
		{
			try
			{
				if (this.HighlightSet == null)
				{
					this.HighlightSet = this.Document.HighlightSets.Item(this.Document.HighlightSets.Count);
				}
				this.HighlightSet.RemoveAll();
			}
			catch { }

			SolidEdgeFramework.Window window = (SolidEdgeFramework.Window)this.Application.ActiveWindow;
			window.View.Update();

		}

		public void RemoveHighlight(Object item)
		{
			Object HighlightItem;
			Object Item;
			System.Type myType = item.GetType();
			if (myType.IsCOMObject) Item = item;
			else Item = ((ISEGeometry)item).getGeometry();

			if (this.HighlightSet != null)
			{
				for (Int32 i = 1; i <= this.HighlightSet.Count; i++)
				{
					HighlightItem = this.HighlightSet.Item(i);
					if (Item.Equals(HighlightItem))
					{
						this.HighlightSet.RemoveItem(i);
						SolidEdgeFramework.Window window = (SolidEdgeFramework.Window)this.Application.ActiveWindow;
						window.View.Update();
						break;
					}
				}
			}
		}

		~SEApplicationWrapper()
		{
			this.HighlightSet = null;
			this.Document = null;
			Marshal.ReleaseComObject(this.Application);
		}

	}


}